﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikebuluo.wxgzpt.wechat.orderfood
{
    public partial class choosefood : System.Web.UI.Page
    {
        a_foodclass_dal foodclassdal = new a_foodclass_dal();
        a_foodlist_dal foodlistdal = new a_foodlist_dal();

        a_orderfood_dal orderfooddal = new a_orderfood_dal();
        a_orderfood_model orderfoodmodel = new a_orderfood_model();

        a_order_dal orderdal = new a_order_dal();
        a_order_model ordermodel = new a_order_model();
        JsonOperate JsonOperate = new JsonOperate();

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.QueryString["action"]) { 
                case"list":
                    list();
                    break;
                case "addmenu":
                    addmenu();
                    break;            
            }
        }

        private void list() {
            DataTable dtfoodclass = foodclassdal.GetList("store_id="+Request.QueryString["sid"]).Tables[0];
            DataTable[] dtfoodlist = new DataTable[100];
            string json = "";
            json += "[";
            for (int i = 0; i < dtfoodclass.Rows.Count; i++)
            {
                dtfoodlist[i] = foodlistdal.GetList("foodclass_id=" + dtfoodclass.Rows[i]["foodclass_id"]).Tables[0];
                string dishes = JsonOperate.TableToBasicJson(dtfoodlist[i]);
                if (i == (dtfoodclass.Rows.Count - 1))
                {
                    json += "{\"foodclass_id\":\"" + dtfoodclass.Rows[i]["foodclass_id"] + "\",";
                    json += "\"foodclass_name\":\"" + dtfoodclass.Rows[i]["foodclass_name"] + "\",";
                    json += "\"store_id\":\"" + dtfoodclass.Rows[i]["store_id"] + "\",";
                    json += "\"foodclass_sort\":\"" + dtfoodclass.Rows[i]["foodclass_sort"] + "\",";
                    json += "\"dishes\":" + JsonOperate.TableToBasicJson(dtfoodlist[i]);
                    json += "}";
                }
                else {
                    json += "{\"foodclass_id\":\"" + dtfoodclass.Rows[i]["foodclass_id"] + "\",";
                    json += "\"foodclass_name\":\"" + dtfoodclass.Rows[i]["foodclass_name"] + "\",";
                    json += "\"store_id\":\"" + dtfoodclass.Rows[i]["store_id"] + "\",";
                    json += "\"foodclass_sort\":\"" + dtfoodclass.Rows[i]["foodclass_sort"] + "\",";
                    json += "\"dishes\":" + JsonOperate.TableToBasicJson(dtfoodlist[i]);
                    json += "},";
                }

            }
            json += "]";
            Response.Write(json);
            Response.End();
        }

        private void addmenu() { 
            string order = Request.Form["order"].Trim().ToString();
            DataTable dt =  JsonOperate.JsonToTable(order);
            for (int i = 0; i < dt.Rows.Count; i++) {
                DataTable dtfoodlsit = foodlistdal.GetList1("foodlist_id=" + dt.Rows[i]["dishes_id"].ToString().Trim() + " and wxid='" + Request.QueryString["wxid"] + "' and orderfood_stutas=0").Tables[0];
                
                if (dtfoodlsit.Rows.Count == 0) {
                    orderfoodmodel.orderfood_stutas = 0;
                    orderfoodmodel.foodlist_id = int.Parse(dt.Rows[i]["dishes_id"].ToString().Trim());
                    orderfoodmodel.orderfood_num = int.Parse(dt.Rows[i]["nums"].ToString().Trim());
                    orderfoodmodel.wxid = Request.QueryString["wxid"].Trim().ToString();
                    orderfooddal.Add(orderfoodmodel);
                }
            }
            Response.End();

        }
    }
}